<template>
  <div class="home-container">
    <header class="header">
      <div class="header-text">
        <h1 class="main-title">开启你的大学社团之旅</h1>
        <p class="sub-title">探索精彩社团生活，成就不凡大学时光</p>
      </div>
    </header>

    <div class="content-area">
      <div class="section">
        <div class="section-content">
          <img src="../../assets/tyut-logo.png" alt="多样化社团图标" />
          <h3>多样化的社团</h3>
          <p>在这里，你可以找到志同道合的伙伴</p>
          <p>发现属于你的精彩社团生活</p>
          <el-button type="primary" size="large" @click="viewClubs" class="action-button">
            立即探索
          </el-button>
        </div>
      </div>
      <div class="section">
        <div class="section-content">
          <img src="../../assets/tyut-logo.png" alt="丰富的社团活动图标" />
          <h3>丰富的社团活动</h3>
          <p>参与丰富多彩的社团活动</p>
          <p>让你的大学生活更加精彩</p>
          <el-button type="success" size="large" @click="viewActivities" class="action-button">
            查看活动
          </el-button>
        </div>
      </div>
    </div>

    <!-- 轮播图 -->
    <div class="carousel-section">
      <h2 class="section-title">精彩活动展示</h2>
      <div class="carousel-container">
        <el-carousel height="400px" v-if="imageUrls.length > 0" :interval="4000">
          <el-carousel-item v-for="(url, index) in imageUrls" :key="index">
            <img
              :src="url"
              class="carousel-image"
              alt="轮播图片"
              @error="handleImageError"
            />
          </el-carousel-item>
        </el-carousel>
        <div v-else class="no-images">
          暂无图片
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const router = useRouter()

const viewClubs = () => {
  router.push('/clubs')
}

const viewActivities = () => {
  router.push('/activities')
}

const imageUrls = ref([])

// 获取最新上传的图片信息
const getLatestImages = async () => {
  try {
    const response = await axios.get('/file/latestInHome')
    if (response.data.status === 'success') {
      console.log('Received image URLs:', response.data.data)
      imageUrls.value = response.data.data
    } else {
      ElMessage.warning('暂无图片')
    }
  } catch (error) {
    console.error('获取图片失败:', error)
    ElMessage.error('获取图片失败')
  }
}

// 添加图片加载错误处理
const handleImageError = (e) => {
  console.error('Image load failed:', e.target.src)
  ElMessage.error('图片加载失败')
}

onMounted(() => {
  getLatestImages()
})
</script>

<style scoped>
.home-container {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding: 40px 20px;
}

.header {
  text-align: center;
  padding: 60px 0;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  border-radius: 15px;
  margin-bottom: 40px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.header-text {
  color: white;
}

.main-title {
  font-size: 56px;
  font-weight: bold;
  margin: 0;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.sub-title {
  font-size: 24px;
  margin-top: 20px;
  opacity: 0.9;
}

.content-area {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 30px;
  margin-bottom: 50px;
}

.section {
  background: white;
  border-radius: 20px;
  padding: 40px;
  text-align: center;
  transition: transform 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.section:hover {
  transform: translateY(-5px);
}

.section-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.section img {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  margin-bottom: 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.section h3 {
  font-size: 32px;
  color: #2c3e50;
  margin: 0;
}

.section p {
  font-size: 18px;
  color: #666;
  margin: 5px 0;
  line-height: 1.6;
}

.action-button {
  margin-top: 20px;
  padding: 12px 30px;
  font-size: 18px;
  border-radius: 25px;
  transition: all 0.3s ease;
}

.action-button:hover {
  transform: scale(1.05);
}

.carousel-section {
  padding: 20px 0;
}

.section-title {
  text-align: center;
  font-size: 36px;
  color: #2c3e50;
  margin-bottom: 30px;
}

.carousel-container {
  max-width: 1200px;
  margin: 0 auto;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.carousel-image:hover {
  transform: scale(1.02);
}

.no-images {
  height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  color: #909399;
  font-size: 24px;
  border-radius: 15px;
}

@media (max-width: 768px) {
  .content-area {
    grid-template-columns: 1fr;
  }

  .main-title {
    font-size: 40px;
  }

  .sub-title {
    font-size: 20px;
  }

  .section h3 {
    font-size: 28px;
  }
}
</style>